/**
 * loading提示插件	(2018/03/11) 
 * 提取自EasyUI DataGrid中的加载提示功能
 * 原生JS实现，无依赖，兼容IE
 * @version 1.0
 * @author 范围兄 <ambit_tsai@qq.com>
 * @see https://gitee.com/ambit/easyui_exts/blob/master/loading
 */   
'use strict';
if(!window.loading){
	window.loading = {
		show: function(container, text){
			this._checkArgs(container, text);
			if( !this._container._loading ){
				this.create(container, text);
			}
			this._container._loading.mask.style.display = 'block';
			this._container._loading.msg.style.display = 'block';
		},
		hide: function(container){
			this._checkArgs(container);
			this._container._loading.mask.style.display = 'none';
			this._container._loading.msg.style.display = 'none';
		},
		create: function(container, text){
			this._checkArgs(container, text);
			var mask = document.createElement('div'), 
				msg = document.createElement('div');
			mask.className = 'ambit-loading-mask';
			msg.className = 'ambit-loading-msg';
			msg.innerHTML = this._text;
			this._container.appendChild(mask);
			this._container.appendChild(msg);
			msg.style.marginLeft = (-msg.scrollWidth/2) + 'px';
			this._container._loading = {'mask':mask, 'msg':msg};
		},
		destroy: function(container){
			this._checkArgs(container);
			this._container.removeChild(this._container._loading.msg);
			this._container.removeChild(this._container._loading.mask);
			this._container._loading = null;
		},
		_checkArgs: function(container, text){
			if(typeof container === 'object'){
				this._container = container;
				this._text = text || 'Loading...';
			}else{
				if(typeof container === 'string'){
					this._text = container;
				}else{
					this._text = text || 'Loading...';
				}
				this._container = document.body;
			}
		},
		_container: null,
		_text: null
	};

	// 若存在id为'__SHOW_LOADING__'的元素，将立即提示 
	// 立即提示时，插件需放在<body>中引入 
	if(document.body && document.getElementById('__SHOW_LOADING__')){
		loading.show();
	}
}